package com.ChenYaRou.www.controller;

import com.ChenYaRou.www.constant.JsonBeanConstant;
import com.ChenYaRou.www.dao.UserDao;
import com.ChenYaRou.www.dao.impl.UserDaoImpl;
import com.ChenYaRou.www.po.DetailedUser;
import com.ChenYaRou.www.po.Role;
import com.ChenYaRou.www.po.User;
import com.ChenYaRou.www.utils.*;

import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;

import static com.ChenYaRou.www.constant.Constant.USER;
import static com.ChenYaRou.www.constant.False.EMPTY;

/**
 * @ClassName RegisterServletController
 * @Description TODO
 * @Author 86183
 * @Date 2022/4/30 23:19
 * @Version 1.0
 **/
public class RegisterServletController {
    /**
     * 检验注册时输入的名字是否在数据库中已存在(当用户离开昵称框时输出,校验提交时该信息是否空,空才注册)
     * @param userName 用户输入的昵称
     * @return json
     */
    public String register(String userName){
        if(userName!=null && !EMPTY.equals(userName.trim())){
            String sensitiveFilter = SensitiveFilter.getInstance().sensitiveFilter(userName);
            if(sensitiveFilter!=null){
                return sensitiveFilter;
            }
            try {
                UserDao userDao=new UserDaoImpl();
                User userSameName = userDao.select(userName.substring(1,userName.length()-1));
                //有相同的名字 提示用户更换
                if(userSameName!=null){
                    return JsonBean.generateJson(JsonBeanConstant.NOT_GET,JsonBeanConstant.SAME_NAME,null);
                }
                return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,null);
            } catch (IllegalAccessException | SQLException | NoSuchFieldException | InstantiationException e) {
                e.printStackTrace();
            }
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 此时用户输入的昵称唯一了,所以可以直接注册,返回注册的情况 成功或失败
     * @param user
     * @return
     */
    public String registerFinally(User user){
        if(user!=null){
            try {
                user.setPassword(Secret.turnSecret(user.getPassword()));
                UserDao userDao=new UserDaoImpl();
                int insert = userDao.insert(user);
                if(insert>0){
                    try {
                        User userIn = userDao.select(user.getId());
                        DetailedUser detailedUser=new DetailedUser(userIn.getId(),
                                " "," "," "," ",0F);
                        detailedUser.setPicture("图片.png");
                        String sqlInsertDetailUser = GenerateSql.getInstance().insert(detailedUser);
                        String sqlInsertRole = GenerateSql.getInstance().insert(new Role(userIn.getId(), USER));
                        int i = JdbcUtil.getInstance().insertOrUpdateMore(sqlInsertDetailUser, sqlInsertRole);
                        if(i>0){
                            return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,userIn);
                        }
                    } catch (NoSuchFieldException | InstantiationException e) {
                        e.printStackTrace();
                    }
                }
                return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE,null);
            } catch (IllegalAccessException | NoSuchAlgorithmException | SQLException e) {
                e.printStackTrace();
            }
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }
}
